package com.example.board.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.example.board.mapper.RepurchaseAnalysisMapper;
import com.example.board.pojo.dto.UserDto;
import com.example.board.pojo.vo.*;
import com.example.board.service.RepurchaseAnalysisService;
import com.example.board.util.DateQuery;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
@RequiredArgsConstructor
@Slf4j
public class RepurchaseAnalysisServiceImpl implements RepurchaseAnalysisService {

    private final RepurchaseAnalysisMapper repurchaseAnalysisMapper;

    @Override
    public Map<String, Object> getAllDetail() {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String mothStartLastLast = DateQuery.getMothStartLast2();
        LocalDateTime startDate = LocalDateTime.parse(mothStartLastLast, df);
        LocalDateTime endTime = LocalDateTime.parse(DateQuery.getMothEnd(), df);
        //查询下单总金额 总人数 AllCityAmount 3331, 1469580.00
        AllCityAmount allCityAmount = repurchaseAnalysisMapper.findAllAmount(null, null, startDate, endTime, null);

        //查询3个月用户下单id ,金额
        List<Long> userIds = repurchaseAnalysisMapper.findUserRecord(startDate);

        //查询现在是否下单 总复购金额
        UserDto rate = repurchaseAnalysisMapper.findUserRecordRate(userIds, startDate, endTime, null, null, null);

        Map<String, Object> map = new HashMap<>();
        map.put("customerUnitTotal", allCityAmount.getFinish() / Integer.parseInt(allCityAmount.getTotal()));
        map.put("repurchaseTotal", rate.getOrderAmountAll());
        return map;
    }

    @Override
    public List<SalesmanRepurchaseVo> getSalesmanRepurchase(String province, String region) {
        if (StringUtils.isEmpty(province) && StringUtils.isEmpty(region)) {
            province = "广东省";
            region = "深圳市";
        }
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String mothStartLastLast = DateQuery.getMothStartLast2();
        String mothStart = DateQuery.getMothEnd();
        LocalDateTime startDate = LocalDateTime.parse(mothStartLastLast, df);
        LocalDateTime endDate = LocalDateTime.parse(mothStart, df);

        //查询业务员入职时间 姓名
        List<SalesmanRepurchaseVo> salesmanRepurchaseVo
                = repurchaseAnalysisMapper.findSalesMan(province, region);


        List<Long> userIds = salesmanRepurchaseVo.stream().map(SalesmanRepurchaseVo::getId).collect(Collectors.toList());

        //查询之前是否下单
        List<UserDto> userRe = repurchaseAnalysisMapper.findUserRecordRates(startDate, userIds);
        List<Long> userRecordIds = userRe.stream().map(UserDto::getUserRecordId).collect(Collectors.toList());

        //查销售人员下这三个月复购下单用户
        List<SalesmanRepurchaseVo> salesmanRepurchaseVos = null;
        if (CollectionUtils.isNotEmpty(userRecordIds)) {
            salesmanRepurchaseVos = repurchaseAnalysisMapper.findAllUserRecordIds(userRecordIds, startDate, endDate);
        }


        //查询 总客户数 getPurchaseCustomerNumber
        List<UserDto> userOrder = repurchaseAnalysisMapper.findAllUserOrders(userIds, startDate, endDate);


        List<SalesmanRepurchaseVo> salesmanRepurchaseVoList = new ArrayList<>();

        for (SalesmanRepurchaseVo repurchaseVo : salesmanRepurchaseVo) {

            SalesmanRepurchaseVo salesmanRepurchaseVo1 = new SalesmanRepurchaseVo();

            salesmanRepurchaseVo1.setSalesman(repurchaseVo.getSalesman());
            salesmanRepurchaseVo1.setEntryTime(repurchaseVo.getEntryTime());
            salesmanRepurchaseVo1.setId(repurchaseVo.getId());
            salesmanRepurchaseVo1.setTimeRangeStart(startDate);
            salesmanRepurchaseVo1.setTimeRangeEnd(endDate);

            salesmanRepurchaseVo1.setRepurchaseCustomerNumber(0);
            salesmanRepurchaseVo1.setRepurchaseNumber(0);
            salesmanRepurchaseVo1.setRepurchaseRate(new BigDecimal(0));
            salesmanRepurchaseVo1.setPurchaseCustomerNumber(0);


            if (CollectionUtils.isNotEmpty(salesmanRepurchaseVos)) {
                for (SalesmanRepurchaseVo vo : salesmanRepurchaseVos) {
                    if (vo.getId().equals(repurchaseVo.getId())) {

                        //返单的客户数量 repurchaseCustomerNumber
                        int totalUR = salesmanRepurchaseVos.stream().filter(x -> x.getId().equals(vo.getId()))
                                .mapToInt(SalesmanRepurchaseVo::getRepurchaseCustomerNumber).sum();

                        //下单的件数
                        int totalOrder = salesmanRepurchaseVos.stream().filter(x -> x.getId().equals(vo.getId()))
                                .mapToInt(SalesmanRepurchaseVo::getRepurchaseNumber).sum();
// getRepurchaseNumber
                        salesmanRepurchaseVo1.setRepurchaseCustomerNumber(totalUR);
                        salesmanRepurchaseVo1.setRepurchaseNumber(totalOrder);

                    }
                }
            }
            if (CollectionUtils.isNotEmpty(userOrder)) {
                for (UserDto userDto : userOrder) {
                    if (userDto.getId().equals(repurchaseVo.getId())) {
                        salesmanRepurchaseVo1.setPurchaseCustomerNumber(userDto.getTotalOrder());
                    }
                }
            }
            if (salesmanRepurchaseVo1.getPurchaseCustomerNumber() != 0) {
                salesmanRepurchaseVo1.setRepurchaseRate(new BigDecimal(salesmanRepurchaseVo1.getRepurchaseCustomerNumber())
                        .divide(new BigDecimal(salesmanRepurchaseVo1.getPurchaseCustomerNumber()), 2, RoundingMode.HALF_UP));
            }


            salesmanRepurchaseVoList.add(salesmanRepurchaseVo1);
        }

        return salesmanRepurchaseVoList.stream().sorted(Comparator.comparingInt(SalesmanRepurchaseVo::getPurchaseCustomerNumber).reversed()).collect(Collectors.toList());
    }

    @Override
    public Map<String, Object> getTeamRepurchase(String province, String region, String teamName) {
        if (StringUtils.isEmpty(province) && StringUtils.isEmpty(region)) {
            province = "广东省";
            region = "深圳市";
        }
        //查询订单收货地址
//        repurchaseAnalysisMapper.findUserRecordRate() 8月
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String mothStartLastLast = DateQuery.getMothStartLast2();
        String mothStartLsatLast = DateQuery.getMothEndLast2();
        LocalDateTime startDateLastLast = LocalDateTime.parse(mothStartLastLast, df);
        LocalDateTime endDateLastLast = LocalDateTime.parse(mothStartLsatLast, df);
        String mothLastLast = (Integer.parseInt((DateTime.now() + "").split(" ")[0].split("-")[1]) - 2) + "月";
        TeamRepurchase rateLastLast = getRate(province, region, startDateLastLast, endDateLastLast, mothLastLast, teamName);

        //9月
        LocalDateTime startDateLast = LocalDateTime.parse(DateQuery.getMothStartLast(), df);
        LocalDateTime endDateLast = LocalDateTime.parse(DateQuery.getMothEndLast(), df);
        String mothLast = (Integer.parseInt((DateTime.now() + "").split(" ")[0].split("-")[1]) - 1) + "月";
        TeamRepurchase rateLast = getRate(province, region, startDateLast, endDateLast, mothLast, teamName);

        //10月
        LocalDateTime endTime = LocalDateTime.parse(DateQuery.getMothEnd(), df);
        LocalDateTime startTime = LocalDateTime.parse(DateQuery.getMothStart(), df);
        String moth = (Integer.parseInt((DateTime.now() + "").split(" ")[0].split("-")[1])) + "月";
        TeamRepurchase rate = getRate(province, region, startTime, endTime, moth, teamName);

        List<TeamRepurchase> list = new ArrayList<>();
        list.add(rateLastLast);
        list.add(rateLast);
        list.add(rate);


        TotalStaticVo totalStaticVo = new TotalStaticVo();

        //三月合计
        //复购件数
        int totalNumber = rateLastLast.getRepurchaseNumber() + rateLast.getRepurchaseNumber() + rate.getRepurchaseNumber();
        //复购金额
        Double totalAmount = rateLastLast.getRepurchaseMoney() + rateLast.getRepurchaseMoney() + rate.getRepurchaseMoney();
        //成交客户
        int target = rateLastLast.getTarget() + rateLast.getTarget() + rate.getTarget();
        //复购客户
        int current = rateLastLast.getCurrent() + rateLast.getCurrent() + rate.getCurrent();
        //复购率
        BigDecimal rates = new BigDecimal(0);
        if (target != 0) {
            rates = new BigDecimal(current).divide(new BigDecimal(target), 2, RoundingMode.HALF_UP);
        }

        totalStaticVo.setPurchaseCustomer(target);
        totalStaticVo.setRepurchaseCustomer(current);
        totalStaticVo.setRepurchaseMoney(totalAmount);
        totalStaticVo.setRepurchaseRate(rates);
        totalStaticVo.setRepurchaseNumber(totalNumber);
        totalStaticVo.setNoRepurchaseRate(new BigDecimal(1).subtract(rates));
        totalStaticVo.setNoRepurchaseCustomer(target - current);


        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotEmpty(province) && StringUtils.isEmpty(region)) {
            region = province;
        }
        if (StringUtils.isNotEmpty(teamName)) {
            region = teamName;
        }
        map.put("city", region);
        map.put("totalStatic", totalStaticVo);
        map.put("chartsData", list);

        return map;
    }

    public TeamRepurchase getRate(String province, String region, LocalDateTime startDate, LocalDateTime endDate, String moth, String teamName) {
        //8月 获取两个月前的时间
        TeamRepurchase teamRepurchase = new TeamRepurchase();

        //查询8月之前购买人 id
        List<Long> userRecordId = repurchaseAnalysisMapper.findTimesUserRecord(startDate, province, region, teamName);
        if (CollectionUtils.isNotEmpty(userRecordId)) {
            //复购件数 金额
            UserDto userRecordRate = repurchaseAnalysisMapper.findUserRecordRate(userRecordId, startDate, endDate, province, region, teamName);

            //查询8月总金额 单数
            AllCityAmount allCityAmount = repurchaseAnalysisMapper.findAllAmount(province, region, startDate, endDate, teamName);

            teamRepurchase.setCurrent(0);
            teamRepurchase.setMonth(moth);
            teamRepurchase.setPercent(new BigDecimal(0));
            teamRepurchase.setRepurchaseMoney(0.00);
            teamRepurchase.setRepurchaseNumber(0);
            teamRepurchase.setRepurchaseRate(new BigDecimal(0));
            teamRepurchase.setTarget(0);
            teamRepurchase.setNoRepurchaseRate(new BigDecimal(0));
            teamRepurchase.setTotalOrder(0);
            teamRepurchase.setNoCurrent(0);

            if (ObjectUtils.isNotEmpty(userRecordRate) && userRecordRate.getOrderAmountAll() != null) {
                teamRepurchase.setCurrent(userRecordRate.getCustomer());
                teamRepurchase.setRepurchaseMoney(userRecordRate.getOrderAmountAll());
                teamRepurchase.setRepurchaseNumber(userRecordRate.getTotalOrder());
            }
            if (ObjectUtils.isNotEmpty(allCityAmount)) {
                teamRepurchase.setTarget(Integer.parseInt(allCityAmount.getTotal()));
                teamRepurchase.setTotalOrder(allCityAmount.getTotalOrder());
            }
            teamRepurchase.setNoCurrent(teamRepurchase.getTarget()-teamRepurchase.getCurrent());
            if (teamRepurchase.getTarget() != 0) {
                teamRepurchase.setRepurchaseRate(new BigDecimal(teamRepurchase.getCurrent())
                        .divide(new BigDecimal(teamRepurchase.getTarget()), 2, RoundingMode.HALF_UP));
                teamRepurchase.setNoRepurchaseRate(new BigDecimal(1).subtract(teamRepurchase.getRepurchaseRate()));
                teamRepurchase.setPercent(new BigDecimal(teamRepurchase.getCurrent())
                        .divide(new BigDecimal(teamRepurchase.getTarget()), 2, RoundingMode.HALF_UP));
            }
            return teamRepurchase;
        } else {

            teamRepurchase.setCurrent(0);
            teamRepurchase.setMonth(moth);
            teamRepurchase.setPercent(new BigDecimal(0));
            teamRepurchase.setRepurchaseMoney(0.00);
            teamRepurchase.setRepurchaseNumber(0);
            teamRepurchase.setRepurchaseRate(new BigDecimal(0));
            teamRepurchase.setTarget(0);
            teamRepurchase.setNoRepurchaseRate(new BigDecimal(0));
            teamRepurchase.setTotalOrder(0);

            return teamRepurchase;
        }

    }

    @Override
    public Map<String, Object> getTeamCustomerUnit(String province, String region) {

        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //8月
        String mothStartLastLast = DateQuery.getMothStartLast2();
        String mothStartLsatLast = DateQuery.getMothEndLast2();
        LocalDateTime startDateLastLast = LocalDateTime.parse(mothStartLastLast, df);
        LocalDateTime endDateLastLast = LocalDateTime.parse(mothStartLsatLast, df);
        String mothLastLast = (Integer.parseInt((DateTime.now() + "").split(" ")[0].split("-")[1]) - 2) + "月";


        //9月
        LocalDateTime startDateLast = LocalDateTime.parse(DateQuery.getMothStartLast(), df);
        LocalDateTime endDateLast = LocalDateTime.parse(DateQuery.getMothEndLast(), df);
        String mothLast = (Integer.parseInt((DateTime.now() + "").split(" ")[0].split("-")[1]) - 1) + "月";


        //10月
        LocalDateTime endTime = LocalDateTime.parse(DateQuery.getMothEnd(), df);
        LocalDateTime startTime = LocalDateTime.parse(DateQuery.getMothStart(), df);
        String moth = (Integer.parseInt((DateTime.now() + "").split(" ")[0].split("-")[1])) + "月";

//        List<Map<String, Object>> unit = getUnit(province, region, startDateLastLast, endDateLastLast, mothLastLast);
//        List<Map<String, Object>> unit1 = getUnit(province, region, startDateLast, endDateLast, mothLast);
//        List<Map<String, Object>> unit2 = getUnit(province, region, startTime, endTime, moth);
        List<Map<String, Object>> unit = getUnit(province, region, startDateLastLast, startDateLast, mothLastLast);
        List<Map<String, Object>> unit1 = getUnit(province, region, startDateLast, startTime, mothLast);
        List<Map<String, Object>> unit2 = getUnit(province, region, startTime, endTime, moth);

        //合计
        //总客户
        int totalNumber = (int) unit.get(0).get("value") + (int) unit1.get(0).get("value") + (int) unit2.get(0).get("value");
        int totalMoney = (int) unit.get(1).get("value") + (int) unit1.get(1).get("value") + (int) unit2.get(1).get("value");
        //单价
        int rate = 0;
        if (totalNumber != 0) {
            rate = totalMoney / totalNumber;
        }
        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("title", "合计");
        objectMap.put("type", "group");

        Map<String, Object> numMap = new HashMap<>();
        numMap.put("title", "客户数");
        numMap.put("value", totalNumber);
        numMap.put("type", "item");
        Map<String, Object> moneyMap = new HashMap<>();
        moneyMap.put("title", "总金额");
        moneyMap.put("value", totalMoney);
        moneyMap.put("type", "item");
        Map<String, Object> oneMap = new HashMap<>();
        oneMap.put("title", "客单价");
        oneMap.put("value", rate);
        oneMap.put("type", "item");

        List<Map<String, Object>> unit3 = new ArrayList<>();
        List<Map<String, Object>> unit4 = new ArrayList<>();
        List<Map<String, Object>> unit5 = new ArrayList<>();


        Map<String, Object> numMap1 = new HashMap<>();
        numMap1.put("title", unit.get(0).get("title"));
        numMap1.put("value", unit.get(0).get("value"));
        numMap1.put("type", "item");
        Map<String, Object> numMap2 = new HashMap<>();
        numMap2.put("title", unit1.get(0).get("title"));
        numMap2.put("value", unit1.get(0).get("value"));
        numMap2.put("type", "item");
        Map<String, Object> numMap3 = new HashMap<>();
        numMap3.put("title", unit2.get(0).get("title"));
        numMap3.put("value", unit2.get(0).get("value"));
        numMap3.put("type", "item");
        unit3.add(numMap1);
        unit3.add(numMap2);
        unit3.add(numMap3);


        Map<String, Object> numMap4 = new HashMap<>();
        numMap4.put("title", unit.get(1).get("title"));
        numMap4.put("value", unit.get(1).get("value"));
        numMap4.put("type", "item");
        Map<String, Object> numMap5 = new HashMap<>();
        numMap5.put("title", unit1.get(1).get("title"));
        numMap5.put("value", unit1.get(1).get("value"));
        numMap5.put("type", "item");
        Map<String, Object> numMap6 = new HashMap<>();
        numMap6.put("title", unit2.get(1).get("title"));
        numMap6.put("value", unit2.get(1).get("value"));
        numMap6.put("type", "item");
        unit4.add(numMap4);
        unit4.add(numMap5);
        unit4.add(numMap6);

        Map<String, Object> numMap7 = new HashMap<>();
        numMap7.put("title", unit.get(2).get("title"));
        numMap7.put("value", unit.get(2).get("value"));
        numMap7.put("type", "item");
        Map<String, Object> numMap8 = new HashMap<>();
        numMap8.put("title", unit1.get(2).get("title"));
        numMap8.put("value", unit1.get(2).get("value"));
        numMap8.put("type", "item");
        Map<String, Object> numMap9 = new HashMap<>();
        numMap9.put("title", unit2.get(2).get("title"));
        numMap9.put("value", unit2.get(2).get("value"));
        numMap9.put("type", "item");
        unit5.add(numMap7);
        unit5.add(numMap8);
        unit5.add(numMap9);


//        List<Map<String, Object>> descItems = Stream.of(unit, unit1, unit2).flatMap(Collection::stream).collect(Collectors.toList());
        List<Map<String, Object>> descItems = Stream.of(unit3, unit4, unit5).flatMap(Collection::stream).collect(Collectors.toList());
        descItems.add(objectMap);
        descItems.add(numMap);
        descItems.add(moneyMap);
        descItems.add(oneMap);


        List<Map<String, Object>> chartData = new ArrayList<>();
//        Map<String, Object> orders = getOrders(province, region, startDateLastLast, endDateLastLast, mothLastLast);
//        Map<String, Object> orders1 = getOrders(province, region, startDateLast, endDateLast, mothLast);
//        Map<String, Object> orders2 = getOrders(province, region, startTime, endTime, moth);
//        chartData.add(orders);
//        chartData.add(orders1);
//        chartData.add(orders2);

        Map<String, Object> numMaps = new HashMap<>();
        numMaps.put("title", mothLastLast);
        numMaps.put("data1", unit.get(0).get("value"));
        numMaps.put("data2", unit.get(0).get("orders"));
        chartData.add(numMaps);

        Map<String, Object> numMaps1 = new HashMap<>();
        numMaps1.put("title", mothLast);
        numMaps1.put("data1", unit1.get(0).get("value"));
        numMaps1.put("data2", unit1.get(0).get("orders"));
        chartData.add(numMaps1);

        Map<String, Object> numMaps2 = new HashMap<>();
        numMaps2.put("title", moth);
        numMaps2.put("data1", unit2.get(0).get("value"));
        numMaps2.put("data2", unit2.get(0).get("orders"));
        chartData.add(numMaps2);

        Map<String, Object> stringObjectMap = new HashMap<>();
        if (StringUtils.isEmpty(region) && StringUtils.isEmpty(province)) {
            region = "全国";
        }
        if (StringUtils.isEmpty(region) && StringUtils.isNotEmpty(province)) {
            region = province;
        }
        stringObjectMap.put("city", region);
        stringObjectMap.put("descItems", descItems);
        stringObjectMap.put("chartData", chartData);

        return stringObjectMap;
    }

    public List<Map<String, Object>> getUnit(String province, String region, LocalDateTime startDate, LocalDateTime endDate, String name) {
//        //查询8月客户数量
//        Integer numberTotal = repurchaseAnalysisMapper.findUserNumber(province, region, startDate, endDate);
//        //查询8月总金额
//        Integer moneyTotal = repurchaseAnalysisMapper.findUserMoney(province, region, startDate, endDate);
//
//        //查询8月订单数
//        int totalOrder = repurchaseAnalysisMapper.findUserM(province, region, startDate, endDate) ;

        AllCityAmount allAmount = repurchaseAnalysisMapper.findAllAmount(province, region, startDate, endDate, null);

        int  numberTotal = 0;
        int  moneyTotal = 0;
        //客单价
        int totalOne = 0;
        int totalOrder = 0;

        if (ObjectUtils.isNotEmpty(allAmount)) {
            numberTotal = Integer.parseInt(allAmount.getTotal());
            totalOrder = allAmount.getTotalOrder();
            moneyTotal = allAmount.getFinish();

            if (numberTotal != 0) {
                totalOne = moneyTotal / numberTotal;
            }
        }
            Map<String, Object> numMap = new HashMap<>();
            numMap.put("title", name + "客户数");
            numMap.put("orders",totalOrder);
            numMap.put("value", numberTotal);

            numMap.put("type", "item");
            Map<String, Object> moneyMap = new HashMap<>();
            moneyMap.put("title", name + "总金额");

            moneyMap.put("value", moneyTotal);


            moneyMap.put("type", "item");
            Map<String, Object> oneMap = new HashMap<>();
            oneMap.put("title", name + "客单价");
            oneMap.put("value", totalOne);
            oneMap.put("type", "item");

            List<Map<String, Object>> list = new ArrayList<>();
            list.add(numMap);
            list.add(moneyMap);
            list.add(oneMap);
            return list;

//        if (numberTotal == null) {
//            numberTotal = 0;
//        }
//        if (moneyTotal == null) {
//            moneyTotal = 0;
//        }
//
//        //客单价
//        int totalOne = 0;
//        if (numberTotal != 0) {
//            totalOne = moneyTotal / numberTotal;
//        }
//        Map<String, Object> numMap = new HashMap<>();
//        numMap.put("title", name + "客户数");
//        numMap.put("orders",totalOrder);
//        numMap.put("value", numberTotal);
//
//        numMap.put("type", "item");
//        Map<String, Object> moneyMap = new HashMap<>();
//        moneyMap.put("title", name + "总金额");
//
//        moneyMap.put("value", moneyTotal);
//
//
//        moneyMap.put("type", "item");
//        Map<String, Object> oneMap = new HashMap<>();
//        oneMap.put("title", name + "客单价");
//        oneMap.put("value", totalOne);
//        oneMap.put("type", "item");
//
//        List<Map<String, Object>> list = new ArrayList<>();
//        list.add(numMap);
//        list.add(moneyMap);
//        list.add(oneMap);
//        return list;
    }

//    public Map<String, Object> getOrders(String province, String region, LocalDateTime startDate, LocalDateTime endDate, String name) {
//        //查询8月客户数量
//        int numberTotal = repurchaseAnalysisMapper.findUserNumber(province, region, startDate, endDate) == null ?
//                0 : repurchaseAnalysisMapper.findUserNumber(province, region, startDate, endDate);
//        //查询8月订单数
//        int moneyTotal = repurchaseAnalysisMapper.findUserM(province, region, startDate, endDate) == null
//                ? 0 : repurchaseAnalysisMapper.findUserM(province, region, startDate, endDate);
//
//        Map<String, Object> numMap = new HashMap<>();
//        numMap.put("title", name);
//        numMap.put("data1", numberTotal);
//        numMap.put("data2", moneyTotal);
//
//        return numMap;
//    }


    @Override
    public Map<String, Object> getThreeMonthTotal() {
        //定一个总数
        int targert = 500;
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //8月
        LocalDateTime start = LocalDateTime.parse(DateQuery.getMothStartLast2(), df);
        LocalDateTime endTime = LocalDateTime.parse(DateQuery.getMothEnd(), df);
//        repurchaseAnalysisMapper.find
        TeamRepurchase rate = getRate(null, null, start, endTime, null, null);
        //复购客户数
        Integer current = rate.getCurrent();
        //复购率
        BigDecimal repurchaseRate = rate.getRepurchaseRate();
        //成交客户数
        int target = rate.getTarget();
        //下单数
        int orders = rate.getTotalOrder();

        Map<String, Object> map = new HashMap<>();
        map.put("finish", target);
        map.put("finishRate", new BigDecimal(orders).divide(new BigDecimal(targert), 2, RoundingMode.HALF_UP));
        map.put("repurchase", current);
        map.put("totalRepurchase", current);
        map.put("repurchaseRate", repurchaseRate);

        return map;
    }

    @Override
    public Map<String, Object> getNoRepurchaseTable(String province, String region) {
        List<NoRepurchaseTable> noList = new ArrayList<>();
        List<SalesmanRepurchaseVo> salesmanRepurchase = getSalesmanRepurchase(province, region);

        //查出地址和部门
        List<Long> collect = salesmanRepurchase.stream().map(SalesmanRepurchaseVo::getId).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(collect)) {

            List<NoRepurchaseTable> noRepurchaseTables = repurchaseAnalysisMapper.findSalesManDetail(collect);
            for (SalesmanRepurchaseVo salesmanRepurchaseVo : salesmanRepurchase) {
                for (NoRepurchaseTable noRepurchaseTable : noRepurchaseTables) {
                    if (salesmanRepurchaseVo.getId().equals(noRepurchaseTable.getId())) {
                        NoRepurchaseTable noRepurchaseTable1 = new NoRepurchaseTable();
                        //业务员名称
                        noRepurchaseTable1.setSalesman(noRepurchaseTable.getSalesman());
                        //时间
                        noRepurchaseTable1.setStartTime(salesmanRepurchaseVo.getTimeRangeStart());
                        noRepurchaseTable1.setEndTime(salesmanRepurchaseVo.getTimeRangeEnd());
                        //团队名称
                        noRepurchaseTable1.setTeam(noRepurchaseTable.getCity() + "-" + noRepurchaseTable.getDistrict() + "-" + noRepurchaseTable.getDeptName());
                        //成交客户数

                        if (salesmanRepurchaseVo.getPurchaseCustomerNumber() != null) {
                            noRepurchaseTable1.setPurchaseCustomerNumber(salesmanRepurchaseVo.getPurchaseCustomerNumber());
                            //未复购客户 setPurchaseCustomerNumber
                            noRepurchaseTable1.setNoRepurchaseCustomerNumber(salesmanRepurchaseVo.getPurchaseCustomerNumber() - Long.parseLong(salesmanRepurchaseVo.getRepurchaseCustomerNumber() + ""));
                            //未复购率

                            noRepurchaseTable1.setNoRepurchaseRate(new BigDecimal(1).subtract(salesmanRepurchaseVo.getRepurchaseRate()));


                        } else {
                            noRepurchaseTable1.setPurchaseCustomerNumber(0L);
                            //未复购客户
                            noRepurchaseTable1.setNoRepurchaseCustomerNumber(0L);
                            //未复购率
                            noRepurchaseTable1.setNoRepurchaseRate(new BigDecimal(0));

                        }

                        noList.add(noRepurchaseTable1);
                    }
                }
            }

        }

        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("chartData", noList.stream().sorted(Comparator.comparingLong(NoRepurchaseTable::getPurchaseCustomerNumber).reversed()).collect(Collectors.toList()));

        return objectMap;
    }


    @Override
    public Map<String, Object> getTeamNoRepurchaseTable(String province, String region, String teamName) {

        return getTeamRepurchase(province, region, teamName);
    }
}
